
const { ccclass, property } = cc._decorator;
@ccclass
export default class UILoading extends cc.Component {

	private static _instance: UILoading = null;
	public static get_instance(): UILoading {
		if (UILoading._instance) {
			return UILoading._instance;
		} else {
			console.error("UILoading 没有初始化");
		}
	}

	loading: cc.Node = null

	onLoad() {
		UILoading._instance = this;
		cc.game.addPersistRootNode(this.node);

		this.loading = this.node.getChildByName("loading");

		this.node.active = false;
	}

	update(dt) {
		if (this.node.active) {
			this.loading.angle -= 5;
		}
	}

	showLoding() {
		let uiRoot = cc.director.getScene();
		this.node.parent = uiRoot
		this.node.active = true;
		this.node.zIndex = 9999;

		this.loading.stopAllActions();
		this.loading.opacity = 0;
		cc.tween(this.loading)
			.delay(0.5)
			.to(0.5, { opacity: 255 })
			.start()
	}

	hideLoding() {
		this.node.active = false;
	}

	/***************************************************
	 * 
	 ***************************************************/

	public static ShowLoading() {
		UILoading.get_instance() && UILoading.get_instance().showLoding();
	}

	public static HideLoading() {
		UILoading.get_instance() && UILoading.get_instance().hideLoding();
	}
}

